package com.tianji.common;

import com.tianji.coupon.enums.Code;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class Result<T> implements Serializable {
    @ApiModelProperty("响应编码")
    private int code = Code.ReqSuccess.getCode();
    @ApiModelProperty("响应消息")
    private String msg = Code.ReqSuccess.getMsg();
    @ApiModelProperty("是否成功")
    private boolean success = true;
    @ApiModelProperty("响应数据")
    private T data;

    public Result(){}

    public Result(Code code) {
        if (code == null) {
            return;
        }
        this.code = code.getCode();
        if (!Code.ReqSuccess.equals(code)) {
            this.success = false;
        }
        this.msg = code.getMsg();
    }

    public Result(Code code, String msg) {
        if (code == null) {
            return;
        }
        this.code = code.getCode();
        if (!Code.ReqSuccess.equals(code)) {
            this.success = false;
        }
        this.msg = msg;
    }

    public Result(Code code, String msg, T data) {
        if (code == null) {
            return;
        }
        this.code = code.getCode();
        if (!Code.ReqSuccess.equals(code)) {
            this.success = false;
        }
        this.msg = msg;
        this.data = data;
    }


    public static Result success() {
        return new Result(Code.ReqSuccess);
    }

    public static Result success(String msg) {
        return new Result(Code.ReqSuccess, msg, null);
    }

    public static<T> Result<T> success(T data) {
        return new Result(Code.ReqSuccess, Code.ReqSuccess.getMsg(), data);
    }

    public static<T> Result<T> success(String msg, T data) {
        return new Result<T>(Code.ReqSuccess, msg, data);
    }

    public static Result failure() {
        return new Result(Code.ReqFailure);
    }

    public static Result failure(String msg) {
        return new Result(Code.ReqFailure, msg, null);
    }

    public static Result failure(Code code) {
        return new Result(code, code.getMsg(), null);
    }

    public static<T> Result<T> failure(String msg, T data) {
        return new Result<T>(Code.ReqFailure, msg, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
